<?php
require('includes/top.php');


foreach($_POST as $key => $value)
{
if($value!="" && $value!="Save")
{
$data = $key.'_'.$value;

$assigned_laptops_array = explode('_',$data);
$assigned_id = $assigned_laptops_array[1];
$date = date("d-m-y");
$user_name = $_SESSION['user_name'];
$user_id =  $assigned_laptops_array[2];

$sql="INSERT INTO assigned_laptops(`laptop_id`,`user_id`,`date`,`user_name`) VALUES ('$assigned_id','$user_id','$date','$user_name')";
$query = mysql_query("SELECT laptopmodel,serial_no,dispatch_location FROM laptop where user_id = '".trim($user_id)."'");
$queryresult = mysql_fetch_array($query);

$locquery = mysql_query("SELECT user_email_address FROM users where user_address = '".trim($queryresult['dispatch_location'])."'");
$locqueryresult = mysql_fetch_array($locquery);



// recipients
$to  = $locqueryresult['user_email_address'];
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Laptrak</title>
</head>
<body>
  <p><strong>Following laptops is to be delivered:</strong></p>
  <table>
    <tr>
      <td>Model No :</td><td>'.$queryresult['laptopmodel'].'</td>
    </tr>
    <tr>
      <td>Serial No :</td><td>'.$queryresult['serial_no'].'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From: no-reply@laptrak.com' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);

$result=mysql_query($sql);

$sql1="UPDATE laptop SET `assigned` = '1' WHERE laptop_id = '".trim($assigned_id)."' and user_id = '".trim($user_id)."'";
$result1=mysql_query($sql1);
}
}

if($result>0)
{
header('Location:view_approved_main.php?v=success');
}
else
{
header('Location:view_approved_main.php?v=err');
}
?>